<?php

/**
 * report actions.
 *
 * @package    student_manager
 * @subpackage report
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class mainActions extends sfActions
{
public function executeChangePassword(){
    $this->getResponse()->setTitle('WMS-Change-Password');
    $this->hasError = null;
    $this->hasSuccess = null;
    $this->password = $this->getRequestParameter('password');
    $this->passwordbis = $this->getRequestParameter('passwordbis');
    if (!$this->password){
        return;
    }
    if (strlen($this->password) < 4){
        $this->hasError = 'Length of password must be greater than 3';
        return;
    }
    if ($this->password != $this->passwordbis){
        $this->hasError = 'Password is not same';
        return;
    }
    
    $user = sfGuardUserPeer::retrieveByPK($this->getUser()->getGuardUser()->getId());
    if ($user) {
        $user->setPassword($this->password);
        $user->setPasswordBis($this->passwordbis);
        $user->save();
        $this->hasSuccess = 'Change password successfully!';
    }
}

protected function IsGroupApproved($module, $objectId){
   //get group id of current user
    $c = new Criteria();
    $c->add(sfGuardUserGroupPeer::USER_ID , $this->getUser()->getGuardUser()->getId());
    $userGroup = sfGuardUserGroupPeer::doSelectOne($c);
    if (!$userGroup) return false;
    $groupId = $userGroup->getGroupId();
    
    $c = new Criteria();
    $c->add(ApprovalPeer::TYPE , CommonValues::$gApprovalType['key'][$module]);
    $c->add(ApprovalPeer::OBJECT_ID , $objectId);
    $approvals = ApprovalPeer::doSelect($c);
    //is approved once
    foreach ($approvals as $approval){
        $userId = $approval->getUserId();
        
        $c = new Criteria();
        $c->add(sfGuardUserGroupPeer::USER_ID , $userId);
        $userGroup = sfGuardUserGroupPeer::doSelectOne($c);
        //this user have approved already
        if ($userGroup && $userGroup->getGroupId() == $groupId) 
            return true;
    }
    return false;
}
public function executeShowDashboard(){
   $this->getResponse()->setTitle('WMS-DashBoard');
    if (($this->getUser()->hasCredential('APPROVE_BOM') ) ) {
        
        $c = new Criteria();
        $c->add(BOMPeer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);
        $c->addAscendingOrderByColumn(BOMPeer::PUBLISHTIME );
        $Objectlist = BOMPeer::doSelect($c);
        
        $module = 'BOM';
        $this->ObjectneedApprovalList = array();
        $count = 0;    
            
        //if approve normal
        if ($this->getUser()->hasCredential('APPROVE_BOM')){
            foreach ($Objectlist as $object){
                if ( !$this->IsGroupApproved($module, $object->getId())) {
                    $this->ObjectneedApprovalList[$count] = $object;
                    $count++;
                }
            }
        }
        $this->BOMCount = $count;
    }
    
    
    if (($this->getUser()->hasCredential('APPROVE_MRSUBSTK') || $this->getUser()->hasCredential('APPROVE_MRSUBFTR')) ) {
        
        if ($this->getUser()->hasCredential('APPROVE_MRSUBSTK') && $this->getUser()->hasCredential('APPROVE_MRSUBFTR')) {
            $c = new Criteria();
            $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);    
            $c->addAscendingOrderByColumn(MR2Peer::PUBLISHTIME );
            $Objectlist = MR2Peer::doSelect($c);        
        } elseif ($this->getUser()->hasCredential('APPROVE_MRSUBSTK')){
            $c = new Criteria();
            $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);    
            $c->add(MR2Peer::CATEGORY , CommonValues::$gMRCategory['key']['Stock']);
            $c->addAscendingOrderByColumn(MR2Peer::PUBLISHTIME );
            $Objectlist = MR2Peer::doSelect($c);        
        } elseif ($this->getUser()->hasCredential('APPROVE_MRSUBFTR')){
            $c = new Criteria();
            $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);    
            $c->add(MR2Peer::CATEGORY , CommonValues::$gMRCategory['key']['Factory']);
            $c->addAscendingOrderByColumn(MR2Peer::PUBLISHTIME );
            $Objectlist = MR2Peer::doSelect($c);        
        }
        
        $module = 'MRSUB';
        $this->ObjectneedApprovalList = array();
        $count = 0;    
            
        //if approve normal
        if ($this->getUser()->hasCredential('APPROVE_MRSUBSTK') || $this->getUser()->hasCredential('APPROVE_MRSUBFTR')){                
            foreach ($Objectlist as $object){
                if ( !$this->IsGroupApproved($module, $object->getId())) {
                    $this->ObjectneedApprovalList[$count] = $object;
                    $count++;
                }
            }
        }
        $this->MR2Count = $count;
    }
    
    
    
    if (($this->getUser()->hasCredential('APPROVE_MRRAW') || $this->getUser()->hasCredential('PR_APPROVE_MRRAW')) ) {
        
        $c = new Criteria();
        $c->add(MR3Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);    
        $c->addAscendingOrderByColumn(MR3Peer::PUBLISHTIME );
        $Objectlist = MR3Peer::doSelect($c);
        
        $module = 'MRRAW';
        $this->ObjectneedApprovalList = array();
        $count = 0;    
            
        //if approve normal
        if ($this->getUser()->hasCredential('APPROVE_MRRAW')){                
            foreach ($Objectlist as $object){
                if ( !$this->IsGroupApproved($module, $object->getId())) {
                    $this->ObjectneedApprovalList[$count] = $object;
                    $count++;
                }
            }
        } 
        $this->MR3Count = $count;
    }
    
    
    
    if ($this->getUser()->hasCredential('APPROVE_PO') || $this->getUser()->hasCredential('PR_APPROVE_PO') ) {
        
        $c = new Criteria();
        $c->add(POPeer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);    
        $c->addAscendingOrderByColumn(POPeer::PUBLISHTIME );
        $Objectlist = POPeer::doSelect($c);
        
        $module = 'PO';
        $this->ObjectneedApprovalList = array();
        $count = 0;    
            
        //if approve normal
        if ($this->getUser()->hasCredential('APPROVE_PO')){                
            foreach ($Objectlist as $object){
                if ( !$this->IsGroupApproved($module, $object->getId())) {
                    $this->ObjectneedApprovalList[$count] = $object;
                    $count++;
                }
            }
        }
        $this->POCount = $count;
    }
    
    if ($this->getUser()->hasCredential('CREATE_PO') ) {
        
        $c = new Criteria();
        $c->add(MRPeer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MRPeer::REVISE_ID, 0, Criteria::NOT_EQUAL );
        $c->add(MRPeer::ISRVRESOLVED, 0, Criteria::EQUAL );
        $c->addAscendingOrderByColumn(MRPeer::PUBLISHTIME );              
        
        $RvMRList['MR'] = MRPeer::doSelect($c);
        
        $c = new Criteria();
        $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MR2Peer::REVISE_ID, 0, Criteria::NOT_EQUAL );
        $c->add(MR2Peer::ISRVRESOLVED, 0, Criteria::EQUAL );
        $c->addAscendingOrderByColumn(MR2Peer::PUBLISHTIME );              
        
        $RvMRList['MR2'] = MR2Peer::doSelect($c);
        
        $c = new Criteria();
        $c->add(MR3Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MR3Peer::REVISE_ID, 0, Criteria::NOT_EQUAL );
        $c->add(MR3Peer::ISRVRESOLVED, 0, Criteria::EQUAL );
        $c->addAscendingOrderByColumn(MR3Peer::PUBLISHTIME );              
        
        $RvMRList['MR3'] = MR3Peer::doSelect($c);
        
        $this->RvMRList = $RvMRList;
    }
    
    
    if ($this->getUser()->hasCredential('CREATE_PO') ) {
        
        $c = new Criteria();
        $c->add(MRPeer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MRPeer::STATUS , CommonValues::$gMRStatus['key']['In progress']);
        $c->addAscendingOrderByColumn(MRPeer::PUBLISHTIME );              
        
        $MRList = MRPeer::doSelect($c);
        $MRListCreatePO = null;
        if (count($MRList)){
            foreach ($MRList as $object){
                $c = new Criteria();
                $c->add(POPeer::MRNO, $object->getId());
                $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Main Component']);
                $po = POPeer::doSelectOne($c);
                if (!$po){
                    $MRListCreatePO['MR'][] = $object;
                }
            }
        }
        
        $c = new Criteria();
        $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MR2Peer::STATUS , CommonValues::$gMRStatus['key']['In progress']);
        $c->addAscendingOrderByColumn(MR2Peer::PUBLISHTIME );              
        
        $MRList = MR2Peer::doSelect($c);
        if (count($MRList)){
            foreach ($MRList as $object){
                $c = new Criteria();
                $c->add(POPeer::MRNO, $object->getId());
                $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Sub Component']);
                $po = POPeer::doSelectOne($c);
                if (!$po){
                    $MRListCreatePO['MR2'][] = $object;
                }
            }
        }
        
        $c = new Criteria();
        $c->add(MR3Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);    
        $c->add(MR3Peer::STATUS , CommonValues::$gMRStatus['key']['In progress']);
        $c->addAscendingOrderByColumn(MR3Peer::PUBLISHTIME );              
        
        $MRList = MR3Peer::doSelect($c);
        if (count($MRList)){
            foreach ($MRList as $object){
                $c = new Criteria();
                $c->add(POPeer::MRNO, $object->getId());
                $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Raw Material']);
                $po = POPeer::doSelectOne($c);
                if (!$po){
                    $MRListCreatePO['MR3'][] = $object;
                }
            }
        }
        
        $this->MRListCreatePO = $MRListCreatePO;
    }
    
    $function['mod']=null;
    $function['link']=null;
    if ($this->getUser()->hasCredential('VIEW_BOM') ) {
        $function['mod'][] = 'View BOM';
        $function['link'][] = 'bom/list';
    }
    if ($this->getUser()->hasCredential('CREATE_BOM') ) {
        $function['mod'][] = 'Create BOM';
        $function['link'][] = 'bom/create';
    }
    
    if ($this->getUser()->hasCredential('VIEW_MR') ) {
        $function['mod'][] = 'View MR';
        $function['link'][] = 'mr/list';
    }
//    if ($this->getUser()->hasCredential('CREATE_MR') ) {
//        $function['mod'][] = 'Create MR MAIN';
//        $function['link'][] = 'mr/create';
//    }

    if ($this->getUser()->hasCredential('VIEW_MRSUB') ) {
        $function['mod'][] = 'View MR SUB';
        $function['link'][] = 'mr2/list';
    }
    if ($this->getUser()->hasCredential('CREATE_MRSUB') ) {
        $function['mod'][] = 'Create MR SUB';
        $function['link'][] = 'mr2/create';
    }
    
    if ($this->getUser()->hasCredential('VIEW_MRRAW') ) {
        $function['mod'][] = 'View MR RAW';
        $function['link'][] = 'mr3/list';
    }
    if ($this->getUser()->hasCredential('CREATE_MRRAW') ) {
        $function['mod'][] = 'Create MR RAW';
        $function['link'][] = 'mr3/create';
    }
    
    if ($this->getUser()->hasCredential('VIEW_PO') ) {
        $function['mod'][] = 'View PO';
        $function['link'][] = 'po/list';
    }
    if ($this->getUser()->hasCredential('CREATE_PO') ) {
        $function['mod'][] = 'Create PO/MR';
        $function['link'][] = 'po/SLListJobPurchaser?mod=MR';
    }
    if ($this->getUser()->hasCredential('CREATE_PO') ) {
        $function['mod'][] = 'Create PO/MRSUB';
        $function['link'][] = 'po/SLListJobPurchaser?mod=MR2';
    }
    if ($this->getUser()->hasCredential('CREATE_PO') ) {
        $function['mod'][] = 'Create PO/MRRAW';
        $function['link'][] = 'po/SLListJobPurchaser?mod=MR3';
    }
        
    
    if ($this->getUser()->hasCredential('VIEW_TALLYIN') ) {
        $function['mod'][] = 'View TALLYIN';
        $function['link'][] = 'tallyin/list';
    }
    if ($this->getUser()->hasCredential('CREATE_TALLYIN') ) {
        $function['mod'][] = 'Create TALLYIN';
        $function['link'][] = 'tallyin/create';
    }
        
    
    if ($this->getUser()->hasCredential('VIEW_TALLYOUT') ) {
        $function['mod'][] = 'View TALLYOUT';
        $function['link'][] = 'tallyout/list';
    }
    if ($this->getUser()->hasCredential('CREATE_TALLYOUT') ) {
        $function['mod'][] = 'Create TALLYOUT';
        $function['link'][] = 'tallyout/create';
    }
        
        
    if ($this->getUser()->hasCredential('VIEW_PROJECT') ) {
        $function['mod'][] = 'View PROJECT';
        $function['link'][] = 'project/list';
    }
    if ($this->getUser()->hasCredential('CREATE_PROJECT') ) {
        $function['mod'][] = 'Create PROJECT';
        $function['link'][] = 'project/create';
    }
    
    if ($this->getUser()->hasCredential('VIEW_COMP') ) {
        $function['mod'][] = 'View COMP';
        $function['link'][] = 'component/list';
    }
    if ($this->getUser()->hasCredential('CREATE_COMP') ) {
        $function['mod'][] = 'Create COMP';
        $function['link'][] = 'component/create';
    }
    
    
    if ($this->getUser()->hasCredential('VIEW_COMPUNIT') ) {
        $function['mod'][] = 'View COMPUNIT';
        $function['link'][] = 'componentunit/list';
    }
    if ($this->getUser()->hasCredential('CREATE_COMPUNIT') ) {
        $function['mod'][] = 'Create COMPUNIT';
        $function['link'][] = 'componentunit/create';
    }   
    
        
    if ($this->getUser()->hasCredential('VIEW_COMPTYPE') ) {
        $function['mod'][] = 'View COMPTYPE';
        $function['link'][] = 'componenttype/list';
    }
    if ($this->getUser()->hasCredential('CREATE_COMPTYPE') ) {
        $function['mod'][] = 'Create COMPTYPE';
        $function['link'][] = 'componenttype/create';
    }   
    
        
    if ($this->getUser()->hasCredential('VIEW_COMPSBYCLIENT') ) {
        $function['mod'][] = 'View COMPSBYCLIENT';
        $function['link'][] = 'componentsbyclient/list';
    }
    if ($this->getUser()->hasCredential('CREATE_COMPSBYCLIENT') ) {
        $function['mod'][] = 'Create COMPSBYCLIENT';
        $function['link'][] = 'componentsbyclient/create';
    }    
    
        
    if ($this->getUser()->hasCredential('VIEW_TEMPOUTPUT') ) {
        $function['mod'][] = 'View TEMPOUTPUT';
        $function['link'][] = 'temporaryoutput/list';
    }
    if ($this->getUser()->hasCredential('CREATE_TEMPOUTPUT') ) {
        $function['mod'][] = 'Create TEMPOUTPUT';
        $function['link'][] = 'temporaryoutput/create';
    }
        
    if ($this->getUser()->hasCredential('VIEW_CLIENT') ) {
        $function['mod'][] = 'View CLIENT';
        $function['link'][] = 'client/list';
    }
    if ($this->getUser()->hasCredential('CREATE_CLIENT') ) {
        $function['mod'][] = 'Create CLIENT';
        $function['link'][] = 'client/create';
    }
        
    if ($this->getUser()->hasCredential('VIEW_SUPPLIER') ) {
        $function['mod'][] = 'View SUPPLIER';
        $function['link'][] = 'supplier/list';
    }
    if ($this->getUser()->hasCredential('CREATE_SUPPLIER') ) {
        $function['mod'][] = 'Create SUPPLIER';
        $function['link'][] = 'supplier/create';
    }
    
    {    
        $function['mod'][] = 'View WAREHOUSE';
        $function['link'][] = 'component/ViewWarehouse';
    }
    
    $this->function = $function;
}

}
